package dbcontrol

import (
	"MqttCloudProtocolServer/publiclib/datamodel/dborm"
	"errors"

	uuid "github.com/satori/go.uuid"
)

//UserconfControl struct
type UserconfControl struct {
	base                dbOperabase
	Data                *dborm.Userconf
	ChildrenUserServers []*UserserverControl
}

//CreateUserconfControl 初始化几种指针函数
func CreateUserconfControl() *UserconfControl {
	content := new(UserconfControl)
	content.base.GetData = func() interface{} {
		return content.Data
	}
	content.base.SetData = func(data interface{}) {
		if data != nil {
			content.Data = data.(*dborm.Userconf)
		}
	}
	content.base.GetKey = func() interface{} {
		if content.Data == nil {
			return nil
		}
		return content.Data.Name
	}
	content.base.CreateNew = func() interface{} {
		res := new(dborm.Userconf)
		uuid := uuid.NewV4()
		res.UUID = uuid.String()
		return res
	}
	content.base.CreateNil = func() interface{} {
		return new(dborm.Userconf)
	}
	return content
}

//GetAll 获取全部用户信息
func (_this *UserconfControl) GetAll() ([]*dborm.Userconf, error) {
	res := make([]*dborm.Userconf, 0)
	_, err := _this.base.GetAllByPage(&res, 0, 0, 0)
	return res, err
}

//GetAllByPage 获取全部用户信息.totPage<=0时，计算分页页数,返回值第二个参数为计算或传入的页数
func (_this *UserconfControl) GetAllByPage(totPage, currentPage, countOfPage int) ([]*dborm.Userconf, int, error) {
	res := make([]*dborm.Userconf, 0)
	calTotpage, err := _this.base.GetAllByPage(&res, totPage, currentPage, countOfPage)
	return res, calTotpage, err
}

//CreateNew 创建一条记录
func (_this *UserconfControl) CreateNew() {
	_this.Data = _this.base.CreateNew().(*dborm.Userconf)
}

//FindByName 根据用户名获取一条记录
func (_this *UserconfControl) FindByName(userName string) error {
	res := make([]*dborm.Userconf, 0)
	err := _this.base.FindByWhere(&res, 0, 0, "name = ?", userName)
	if err != nil {
		return err
	} else if len(res) == 0 {
		_this.Data = nil
		return nil
	} else if len(res) > 1 {
		return errors.New("存在同名用户")
	}
	_this.Data = res[0]
	return nil
}

//FindByID 根据主键获取一条记录
func (_this *UserconfControl) FindByID(id string) error {
	return _this.base.FindByID(id)
}

//Save 更新当前条目
func (_this *UserconfControl) Save() error {
	return _this.base.Save()
}

//Delete 删除当前条目
func (_this *UserconfControl) Delete() error {
	return _this.base.Delete()
}

//GetByUserLevel 根据用户等级获取
func (_this *UserconfControl) GetByUserLevel(level int) ([]*dborm.Userconf, error) {
	res := make([]*dborm.Userconf, 0)
	err := _this.base.FindByWhere(&res, 0, 0, "level = ?", level)
	return res, err
}

//GetChildrenUserServers 获取用户-服务器映射
func (_this *UserconfControl) GetChildrenUserServers() error {
	userServer := CreateUserserverControl()
	res, err := userServer.GetByUserName(_this.Data.Name)
	if err != nil || res == nil {
		return err
	}
	_this.ChildrenUserServers = make([]*UserserverControl, len(res))
	for i, item := range res {
		_this.ChildrenUserServers[i] = CreateUserserverControl()
		_this.ChildrenUserServers[i].Data = item
		_this.ChildrenUserServers[i].ParentUser = _this
	}
	return nil
}
